<template>
  <div>
    <div style="width: 100%;text-align: center;margin-bottom: 10px">
      <el-button @click="handleAdd" style="width: 70%" size="mini" type="primary"><i class="el-icon-edit"></i>&nbsp;新增文件夹</el-button>
    </div>
    <el-tree :data="gdzlkFlList" :props="defaultProps" @node-click="handleNodeClick">
      <template v-slot="{ data, node }">
       <div style="width: 100%">
         <i class="iconfont icon-wenjianjia1" style="color: #FBCC30"></i>
          <span style="font-size: 13px;font-weight: 700"> {{ data.flname }}</span>
         <el-popover
           placement="right"
           width="100"
           trigger="hover">
           <div class="caozuo">
             <div @click="handleUpdate(data)" class="caozuo_item"><i class="el-icon-edit-outline"></i>&nbsp;修改</div>
             <div @click="handleDelete(data)" class="caozuo_item"><i class="el-icon-delete"></i>&nbsp;删除</div>
           </div>
           <span style="float: right;margin-right: 10px;width: 30px;text-align: right;font-weight: 700;font-size: 13px;" slot="reference">...</span>
         </el-popover>
       </div>
      </template>
    </el-tree>
    <!-- 添加或修改工单资料库分类对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="70%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="分类名称" prop="flname">
          <el-input v-model="form.flname" placeholder="请输入分类名称" />
        </el-form-item>
        <el-form-item prop="flbm">
          <template slot="label">
            <span>分类编码</span>
            <el-popover
              placement="top-start"
              title="介绍"
              width="200"
              trigger="hover">
              <div>
                描述：<span style="background-color: orange;color: black">当前的分类编码。</span><br>
              </div>
              <i slot="reference" class="el-icon-question"></i>
            </el-popover>
          </template>
          <el-input v-model="form.flbm" placeholder="请输入分类编码" />
        </el-form-item>
        <el-form-item label="排序" prop="px">
          <el-input v-model="form.px" type="number" placeholder="请输入排序" />
        </el-form-item>
        <el-form-item>
          <template slot="label">
            <span>分类权限</span>
            <el-popover
              placement="top-start"
              title="介绍"
              width="200"
              trigger="hover">
              <div>
                描述：<span style="background-color: orange;color: black">哪些用户可看到此文件夹中资源。</span><br>
                <span style="background-color: yellow;color: black">如果不选择默认所有用户都可以看到</span>
              </div>
              <i slot="reference" class="el-icon-question"></i>
            </el-popover>
          </template>
          <el-input v-model="form.qx" readonly placeholder="请选择授权人员">
            <el-button type="primary" slot="prepend" @click.native="selectXcry"> 选择下场人员 </el-button>
          </el-input>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="form.bz" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <UserSelectModal v-if="showUser" :showUserDialog="showUser" ryType="1" @close="closeUser" @selectUser="selectUser"></UserSelectModal>

  </div>
</template>

<script>
import { listGdzlkFl, getGdzlkFl, delGdzlkFl, addGdzlkFl, updateGdzlkFl } from "@/api/gd/gdzlkFl";
import UserSelectModal from '@/views/components/user/UserSelectModal.vue'
export default {
  name: "GdzlkFl",
  components: { UserSelectModal },
  data() {
    return {
      showUser: false,
      buttonLoading: false,
      defaultProps: {
        children: 'children',
        label: 'flname'
      },
      // 总条数
      total: 0,
      // 工单资料库分类表格数据
      gdzlkFlList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        flbm: undefined,
        flname: undefined,
        px: undefined,
        bz: undefined,
        qx: undefined,
        createuserid: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        flbm: [
          { required: true, message: "分类编码不能为空", trigger: "blur" }
        ],
        flname: [
          { required: true, message: "分类名称不能为空", trigger: "blur" }
        ],
        px: [
          { required: true, message: "排序不能为空", trigger: "blur" }
        ],
        bz: [
          { required: true, message: "备注不能为空", trigger: "blur" }
        ],
        qx: [
          { required: true, message: "当前哪些用户不能查看当前菜单不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    selectXcry() {
      this.showUser = true
    },
    closeUser() {
     this.showUser = false
    },
    selectUser(user, type) {
      let ids = user.map(item => item.userId)
      let names = user.map(item => item.nickName)
      // 判断当前选择的是下场负责人还是下场人员
      this.formInfo.qx = ids.toString()
      this.formInfo.qxname = names.toString()
      this.closeUser()
    },
    handleNodeClick(data) {
      console.log(data);
    },
    /** 查询工单资料库分类列表 */
    getList() {
      this.loading = true;
      listGdzlkFl(this.queryParams).then(response => {
        this.gdzlkFlList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加文件夹";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids
      getGdzlkFl(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改工单资料库分类";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateGdzlkFl(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addGdzlkFl(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除工单资料库分类编号为"' + ids + '"的数据项？').then(() => {
        this.loading = true;
        return delGdzlkFl(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        flbm: undefined,
        flname: undefined,
        px: undefined,
        bz: undefined,
        qx: undefined,
        createTime: undefined,
        createBy: undefined,
        createuserid: undefined
      };
      this.resetForm("form");
    }
  }
};
</script>
<style scoped>
  .caozuo {
    width: 100%;
    height: 50px;
    display: flex;
    flex-direction: column;
  }
  .caozuo_item {
    width: 100%;
    height: 25px;
    line-height: 25px;
    padding-left: 5px;
    cursor: pointer;
  }
  .caozuo_item:hover{
    background-color: #F5F7FA;
    font-weight: 700;
    font-size: 14px;
  }
</style>
